package com.zhuangfei.hpuhelper.view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.hpu.bean.NoticeFile;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.listener.DownloadListener;
import com.hpu.service.DownloadService;
import com.hpu.service.LoginService;
import com.hpu.tools.IntentTools;
import com.hpu.tools.NoticeTools;
import com.hpu.tools.ShareTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.hpuhelper.tools.FileTools;
import com.zhuangfei.hpuhelper.tools.ReflectTools;
import com.zhuangfei.toolkit.model.BundleModel;
import com.zhuangfei.toolkit.tools.ActivityTools;
import com.zhuangfei.toolkit.tools.BundleTools;

import cn.lzf.serviceplatform.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class NoticeDetailActivity extends Activity implements DownloadListener{

	//最新公告的id和title
	String id,title;
	
	//显示解析结果的webview
	WebView webView;
	
	//打开其他软件的按钮
	private LinearLayout openLayout;
	
	//文件下载业务
	private DownloadService downloadService;
	
	//登录业务
	private LoginService loginService;
	private LinearLayout hideLayout;
	private TextView downloadTextView;
	
	private LinearLayout backLayout;
	
	//文件的全路径
	private String path;

	int fromItem=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_detail);
		ActivityTools.setTransparent(this);
		initView();
		String fileName=initPath();
		
		adjustFile(fileName);
		
	}
	
	private void initView() {
		fromItem= BundleTools.getFromItem(this,0);
		downloadService=new DownloadService(handler,this);
		loginService=new LoginService(this,handler);

		webView = (WebView)this.findViewById(R.id.webview);
		hideLayout=(LinearLayout) findViewById(R.id.id_download_hidelayout);
		downloadTextView=(TextView) findViewById(R.id.id_download_text);

		backLayout =  (LinearLayout) findViewById(R.id.id_back);

		backLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				goBack();
			}
		});

		openLayout=(LinearLayout) findViewById(R.id.id_notice_open);
		openLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				openFile();
			}
		});
		
	}
	
	protected void goBack() {
		ActivityTools.backActivity(this,NewsTotalActivity.class,
				new BundleModel()
		.setToItem(fromItem));
	}

	private void openFile(){
		Intent intent=IntentTools.getWordFileIntent(this,path);
		if(IntentTools.isIntentAvailable(NoticeDetailActivity.this, intent)){
			startActivity(intent);
		}else Toast.makeText(NoticeDetailActivity.this,"手机上没有可以打开该文件的应用", Toast.LENGTH_SHORT).show();
	}
	
	private void adjustFile(String fileName) {
		if(fileName==null||FileTools.readMessage(fileName)==null||FileTools.readMessage(fileName).trim().equals("")){
			hideLayout.setVisibility(View.VISIBLE);
			downloadTextView.setText("正在下载..");
			System.out.println("id:"+id+",title:"+title);
			downloadService.download(id, title);
		}
		
	}

	/**
	 * 初始化路径
	 * @return 返回的是该文件名
	 */
	private String initPath() {
		id= BundleTools.getString(this,"id","0");
		title= BundleTools.getString(this,"title","");
		String find=FileTools.findMessageFileName(title);
		path=FileTools.getDir(FileTools.NOTICE)+"/"+find;
		return find;
	}


	private String readHtml(String pathString) {
		String data="";
		File file=new File(pathString);
		if(file.exists())
			try {
				file.createNewFile();
				BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
				String line="";
				while((line=br.readLine())!=null){
					data+=line+"";
				}
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		return data;
	}

	DefaultHandler handler=new DefaultHandler(this){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
			case HttpConstants.DOWNLOAD_SUCCESS:
				Log.i("Download",msg.obj.toString());
				NoticeFile noticeFile=(NoticeFile) msg.obj;
				path=FileTools.getDir(FileTools.NOTICE)+"/"+noticeFile.getFileName();
				NoticeTools.downloadFile(NoticeDetailActivity.this, FileTools.getDir(FileTools.NOTICE), noticeFile.getFileName(), noticeFile.getBytes());
//				openFile();
				break;
				
				//VPN过期，重新登录
			case HttpConstants.VPN_TOOLONG:
				if(handler.isIgnoreVpnExpired()){
					break;
				}
				loginService.login(ShareTools.get(NoticeDetailActivity.this, ShareConstants.STORE_USER, "").toString(),
						ShareTools.get(NoticeDetailActivity.this, ShareConstants.STORE_PASSWORD1, "").toString(),
						ShareTools.get(NoticeDetailActivity.this, ShareConstants.STORE_PASSWORD2, "").toString());
				break;
				
			// 登录成功,重新获取
			case HttpConstants.LOGIN_SUCCESS:
				ShareTools.put(NoticeDetailActivity.this, ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
				downloadService.download(id, title);
				break;
			default:
				break;
			}
		
		};
	};
	
	@Override
	public void onBackPressed() {
		goBack();
	}

	@Override
	public void onDownload(int total, int now) {
		if(total==now) hideLayout.setVisibility(View.GONE);
		
		if(total==now&&total!=0) Toast.makeText(this,"下载完成!", Toast.LENGTH_SHORT).show();
	}
}
